﻿using UnityEngine;
using System.Collections;
using UnityEditor;
using System;
using System.IO;
using UnityEditor.SceneManagement;
using System.Collections.Generic;
using System.Text;

/// <summary>
/// 编辑器自定义快捷菜单
/// </summary>
public class QuickMenu : EditorWindow
{
    /// <summary>
    /// 切换到启动场景
    /// </summary>
    [MenuItem("Custom/Goto Setup")]
    public static void GotoSetup()
    {
        EditorSceneManager.OpenScene(Application.dataPath + "/Scene/Setup.unity");
    }
    /// <summary>
    /// 切换到UI编辑场景
    /// 同学们自己写吧？
    /// 切换场景的函数是不是可以抽象呢？
    /// </summary>
    [MenuItem("Custom/Goto UIEditor")]
    public static void GotoUIEditor()
    {
        EditorSceneManager.OpenScene(Application.dataPath + "/Scene/UIEditor.unity");
    }

    /// <summary>
    /// 切换到UI编辑场景
    /// 同学们自己写吧？
    /// 切换场景的函数是不是可以抽象呢？
    /// </summary>
    [MenuItem("Custom/Goto SceneEditor")]
    public static void GotoSceneEditor()
    {
        EditorSceneManager.OpenScene(Application.dataPath + "/Scene/Lesson1.unity");
    }
    /// <summary>
    /// 配置文件放入Resources目录，准备打包
    /// </summary>
    [MenuItem("Custom/ConfigToResources")]
    public static void ConfigToResources()
    {
        var configDirName = "Config";
        var dstPath = Application.dataPath + "/Resources/" + configDirName;
        var srcPath = Application.dataPath + "/../" + configDirName;
        FileUtil.DeleteFileOrDirectory(dstPath);
        //FileUtil.MoveFileOrDirectory(srcPath, dstPath);
        Directory.CreateDirectory(dstPath);
        foreach (var path in Directory.GetFiles(srcPath))
        {
            var name = Path.GetFileName(path);
            var dirName = dstPath + "/" + name + ".bytes";

            // 转换成UTF-8编码
            Stream fs = File.OpenRead(path);
            using (var reader = new StreamReader(fs, Encoding.GetEncoding("gb2312")))
            {
                var all = reader.ReadToEnd();
                using (var writer = new StreamWriter(dirName, false, Encoding.UTF8))
                {
                    writer.Write(all);
                }
            }
            //File.Copy(path, dirName);
        }

        AssetDatabase.Refresh();
    }

    /// <summary>
    /// 编辑器打Android包
    /// </summary>
    [MenuItem("Package/Android")]
    public static void BuildPackageAndroid()
    {
        ConfigToResources();

        List<string> lsit = new List<string>();
        foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
        {
            if (scene.enabled)
            {
                lsit.Add(scene.path);
            }
        }

        BuildPipeline.BuildPlayer(lsit.ToArray(), "../Release/Sample.apk", BuildTarget.Android, BuildOptions.None);
    }
    /// <summary>
    /// 编辑器打PC包
    /// </summary>
    [MenuItem("Package/PC")]
    public static void BuildPackagePC()
    {
        ConfigToResources();

        List<string> lsit = new List<string>();
        foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
        {
            if (scene.enabled)
            {
                lsit.Add(scene.path);
            }
        }

        BuildPipeline.BuildPlayer(lsit.ToArray(), "../Release/SamplePC.exe", BuildTarget.StandaloneWindows, BuildOptions.None);
    }
}
